package com.carlinfo.stu_ques.question.dao.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.carlinfo.stu_ques.common.dao.impl.BaseDaoImpl;
import com.carlinfo.stu_ques.common.util.FileUtil;
import com.carlinfo.stu_ques.demo.pojo.King;
import com.carlinfo.stu_ques.question.dao.IQuestionDao;
import com.carlinfo.stu_ques.question.pojo.Question;

public class questionDaoImpl extends BaseDaoImpl<Question> implements IQuestionDao
{
	/**
	 * 问题保存的文件
	 */
	private File file = new File(this.parentFile,"question.dat");
	/**
	 * 空构造方法，将文件中储存的问题信息读出
	 */
	public questionDaoImpl()
	{
		readFile(file);
	}
	/* 保存一条问题 */
	@Override
	public int saveOneDao(Question question)
	{
		question.setId(id);
		temId = question.getId();
		int id = super.saveOneDao(question);
		writeFile(file);
		return id;
	}
	/* 更新一条问题 */
	public void updateOneDao(Question question)
	{
		temId = question.getId();
		super.updateOneDao(question);
		writeFile(file);
	}
	/* 删除一条问题 */
	public void deleteOneDao(Question question)
	{
		temId = question.getId();
		super.deleteOneDao(question);
		writeFile(file);
	}
	
	@Override
	public boolean dbToFile(String path)
	{
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		try
		{
			Map<Integer, Object> map = new TreeMap<Integer, Object>();
			List<Object> list = new ArrayList<Object>();
			File file = new File(path);
			FileOutputStream fos = new FileOutputStream(file+"\\question.dat");
			FileUtil fileUtil = new FileUtil();
			/* 准备一条sql语句查询所有朝代 */
			String sql = "select * from question;";
			conn = dbUtil.getConnection();
			pst = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			rs = pst.executeQuery();
			int id = 0;
			/* 将数据库中的数据取出来 */
			while(rs.next())
			{
				Question question = new Question();	
				id = rs.getInt("id");
				question.setId(id);
				question.setCreateTime(rs.getTimestamp("createTime"));
				question.setName(rs.getString("name"));
				question.setEndTime(rs.getTimestamp("endTime"));
				question.setScore(rs.getInt("mark"));
				question.setPeople(rs.getInt("peopleNum"));
				/* 将数据按照id放入map中 */
				map.put(id, question);
			}
			/* 将sql语句关闭 */
			pst.close();
			rs.close();
			/* 查询id最大值 */
			sql = "select max(id) from question;";
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			if(rs.next())
			{
				/* 将map和id最大值放入文件 */
				list.add(map);
				list.add(rs.getInt(1)+1);
				System.out.println("最大id："+rs.getInt(1));
			}
			/* 将数据写到文件中去 */
			fileUtil.writeObject(fos, list);
			return true;
		} catch (IOException e)
		{
			e.printStackTrace();
		} catch (SQLException e)
		{
			e.printStackTrace();
		} finally
		{
			/* 关闭链接 */
			dbUtil.closeAll(pst, conn, rs);
		}
		return false;
	}

	@Override
	public boolean fileToDb()
	{	
		List<Question> list = this.selectListDao();
		if(list.size()!=0) {
			Connection conn = null;
			PreparedStatement pst = null;
			/* 每一次存之前,先将表清空 */
			String sql = "truncate table question;";
			/* 获取连接 */
			conn = dbUtil.getConnection();
			try
			{
				pst = conn.prepareStatement(sql);
				pst.executeUpdate();
				pst.close();
				sql = "INSERT INTO `question`(`name`, `mark`, `peopleNum`, `createTime`, `endTime`) VALUES (?, ?, ?, ?, ?)";
				pst = conn.prepareStatement(sql);
				for (Question question : list)
				{
					pst.setObject(1,question.getName());
					pst.setObject(2, question.getScore());
					pst.setObject(3, question.getPeople());
					pst.setObject(4, question.getCreateTime());
					pst.setObject(5, question.getEndTime());
					pst.addBatch();				
				}
				pst.executeBatch();
				pst.clearBatch();
				return true;
			} catch (SQLException e)
			{
				e.printStackTrace();
			} finally
			{
				/* 关闭链接 */
				dbUtil.closeAll(pst, conn, null);
			}
		} else
		{
			System.out.println("该文件是空的，没有数据可以插入");
		}
		return false;
	}
	
}
